/*
 * OpenParts
 * A dynamic-loading components framework for GWT
 * Copyright (C) 2011 Christophe Bouthier  [chris{AT}binary-gastronome{DOT}fr]
 *
 * This work is partially based on work I have done at INRIA (http://www.inria.fr) 
 * in the context of the Qualipso European Project (http://qualipso.org/),
 * The work done at INRIA is Copyright (C) 2006-2011 INRIA
 *
 * This work is distributed under the LGPL version 3
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3
 * as published by the Free Software Foundation. See the GNU
 * Lesser General Public License in LGPL.txt for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package fr.openparts.OpenParts.test.functionnal;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverBackedSelenium;
import org.openqa.selenium.firefox.FirefoxDriver;

import com.thoughtworks.selenium.Selenium;

/**
 * Selenium functional test of Scheduling Commands on the OpenParts Showcase
 * 
 * @author Christophe Bouthier [chris{AT}binary-gastronome{DOT}fr]
 * @creation.datee 05 May 2011
 */
public class SeleniumTestSchedulingCommands {

    @Test
    public void testSchedulingCommands() throws InterruptedException {
        WebDriver driver = new FirefoxDriver();

        String baseUrl = "http://127.0.0.1:8080/";
        Selenium selenium = new WebDriverBackedSelenium(driver, baseUrl);

        // load the page and test it's loaded
        selenium.open("/index.html");
        selenium.waitForPageToLoad("30000");
        assertTrue(selenium.isElementPresent("//body[@id='fr-openparts']"));
        assertTrue(selenium.isTextPresent("OpenParts Showcase"));
        
        // loading the page load the showcase module - test showcase module is loaded
        Thread.sleep(2000);
        assertTrue(selenium.isTextPresent("Scheduling Commands"));
        
        // load the scheduler module and test it's loaded
        selenium.click("//body[@id='fr-openparts']/div[2]/div[2]/div/div[2]/div/button[5]");
        Thread.sleep(2000);
        assertTrue(selenium.isTextPresent("Scheduler Module"));
       
        // check that nothing is shown yet
        assertFalse(selenium.isTextPresent("-> service 1 loaded"));
        assertFalse(selenium.isTextPresent("View 1"));
        
        // launch the commands
        selenium.click("//div[@id='OP-schedulermodule-schedulermodule-display1']/div/div[2]/div/button");
        
        // wait for all 36 commands to execute. 
        for (int second = 0;; second++) {
            if (second >= 60) fail("timeout");
            try { 
                if (selenium.isElementPresent("//div[@id='schedulerCommandsLogs']/div[36]")) {
                    break; 
                }
            } catch (Exception e) {}
            Thread.sleep(1000);
        }
        
        // now check that they executed in the correct order
        assertEquals("-> service 1 loaded", selenium.getText("//div[@id='schedulerCommandsLogs']/div[1]"));
        assertEquals("-> service 1 view displayed", selenium.getText("//div[@id='schedulerCommandsLogs']/div[2]"));
        assertEquals("-> service 2 loaded", selenium.getText("//div[@id='schedulerCommandsLogs']/div[3]"));
        assertEquals("-> service 2 view displayed", selenium.getText("//div[@id='schedulerCommandsLogs']/div[4]"));
        assertEquals("-> service 3 loaded", selenium.getText("//div[@id='schedulerCommandsLogs']/div[5]"));
        assertEquals("-> service 3 view displayed", selenium.getText("//div[@id='schedulerCommandsLogs']/div[6]"));
        assertEquals("-> service container loaded", selenium.getText("//div[@id='schedulerCommandsLogs']/div[7]"));
        assertEquals("-> service container view displayed", selenium.getText("//div[@id='schedulerCommandsLogs']/div[8]"));
        assertEquals("-> failed to load service wrongservice1 from service 1 view", selenium.getText("//div[@id='schedulerCommandsLogs']/div[9]"));
        assertEquals("-> failed to load service wrongservice1 from service 2 view", selenium.getText("//div[@id='schedulerCommandsLogs']/div[10]"));
        assertEquals("-> service 1 sub loaded", selenium.getText("//div[@id='schedulerCommandsLogs']/div[11]"));
        assertEquals("-> service 1 sub view displayed", selenium.getText("//div[@id='schedulerCommandsLogs']/div[12]"));
        assertEquals("-> handling generic event service1", selenium.getText("//div[@id='schedulerCommandsLogs']/div[13]"));
        assertEquals("-> service 1 sub handling event", selenium.getText("//div[@id='schedulerCommandsLogs']/div[14]"));
        assertEquals("-> failed to load service wrongservice2 from service 2 presenter", selenium.getText("//div[@id='schedulerCommandsLogs']/div[15]"));
        assertEquals("-> failed to load service wrongservice2 from service 3 presenter", selenium.getText("//div[@id='schedulerCommandsLogs']/div[16]"));
        assertEquals("-> service 2 sub loaded", selenium.getText("//div[@id='schedulerCommandsLogs']/div[17]"));
        assertEquals("-> service 2 sub handling event", selenium.getText("//div[@id='schedulerCommandsLogs']/div[18]"));
        assertEquals("-> service 2 sub view displayed", selenium.getText("//div[@id='schedulerCommandsLogs']/div[19]"));
        assertEquals("-> handling generic event service2", selenium.getText("//div[@id='schedulerCommandsLogs']/div[20]"));
        assertEquals("-> service 3 sub loaded", selenium.getText("//div[@id='schedulerCommandsLogs']/div[21]"));
        assertEquals("-> service 3 sub handling event", selenium.getText("//div[@id='schedulerCommandsLogs']/div[22]"));
        assertEquals("-> service embedded loaded", selenium.getText("//div[@id='schedulerCommandsLogs']/div[23]"));
        assertEquals("-> service embedded view displayed", selenium.getText("//div[@id='schedulerCommandsLogs']/div[24]"));
        assertEquals("-> handling generic event service container", selenium.getText("//div[@id='schedulerCommandsLogs']/div[25]"));
        assertEquals("-> service embedded handling message from container slot", selenium.getText("//div[@id='schedulerCommandsLogs']/div[26]"));
        assertEquals("-> service 1 sub sub loaded", selenium.getText("//div[@id='schedulerCommandsLogs']/div[27]"));
        assertEquals("-> service 1 sub sub handling event", selenium.getText("//div[@id='schedulerCommandsLogs']/div[28]"));
        assertEquals("-> failed to load service wrongservice3 from service 1 sub sub presenter", selenium.getText("//div[@id='schedulerCommandsLogs']/div[29]"));
        assertEquals("-> failed to load service wrongservice3 from service 2 sub sub presenter", selenium.getText("//div[@id='schedulerCommandsLogs']/div[30]"));
        assertEquals("-> service 1 sub sub view displayed", selenium.getText("//div[@id='schedulerCommandsLogs']/div[31]"));
        assertEquals("-> service 2 sub sub loaded", selenium.getText("//div[@id='schedulerCommandsLogs']/div[32]"));
        assertEquals("-> service 2 sub sub handling event", selenium.getText("//div[@id='schedulerCommandsLogs']/div[33]"));
        assertEquals("-> service 2 sub sub view displayed", selenium.getText("//div[@id='schedulerCommandsLogs']/div[34]"));
        assertEquals("-> service container handling message from embedded view", selenium.getText("//div[@id='schedulerCommandsLogs']/div[35]"));
        assertEquals("-> handling generic event service embedded", selenium.getText("//div[@id='schedulerCommandsLogs']/div[36]"));

        // check that all views are displayed
        assertTrue(selenium.isTextPresent("View 1"));
        assertTrue(selenium.isTextPresent("EmbeddedView 1"));
        assertTrue(selenium.isTextPresent("EmbeddedSubView 1"));
        assertTrue(selenium.isTextPresent("View 2"));
        assertTrue(selenium.isTextPresent("EmbeddedView 2"));
        assertTrue(selenium.isTextPresent("EmbeddedSubView 2"));
        assertTrue(selenium.isTextPresent("View 3"));
        assertTrue(selenium.isTextPresent("No embedded view here"));
        assertTrue(selenium.isTextPresent("View Container"));        
        assertTrue(selenium.isTextPresent("View Embedded"));

        // check that modifications done through onAttach are displayed
        assertTrue(selenium.isTextPresent("modification by embedded view"));
        assertTrue(selenium.isTextPresent("modification by container view"));
        
        selenium.stop();
    }

}
